package org.jsola.hr.web;

import javax.validation.Valid;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.hr.constant.ConfigCodeConstants;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.attendrule.AttendRuleAddDTO;
import org.jsola.hr.dto.attendrule.AttendRuleUpdateDTO;
import org.jsola.hr.entity.TaskDO;
import org.jsola.hr.provider.ISiteConfigProviderService;
import org.jsola.hr.provider.IUserProviderService;
import org.jsola.hr.query.AttendDepInfoTreeQuery;
import org.jsola.hr.query.AttendEmpInfoQuery;
import org.jsola.hr.query.AttendRuleQuery;
import org.jsola.hr.query.CheckStaffInAttendPlanQuery;
import org.jsola.hr.service.IAttendRuleService;
import org.jsola.hr.service.IAttendSysNoticeTaskService;
import org.jsola.hr.service.ITaskService;
import org.jsola.hr.vo.*;
import org.jsola.message.dto.WeiXinDTO;
import org.jsola.message.dto.WxMiNiParam;
import org.jsola.message.dto.WxTemplateParam;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.jsola.user.entity.UnionOpenIdDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 考勤规则
 *
 * @author wxz
 */
@RestController("hrAttendRuleController")
@RequestMapping(HrConstants.API_PATH + "/v1/attendRule")
@Slf4j
public class AttendRuleController implements IAttendRuleControllerAPI {

    @Autowired
    private IAttendRuleService attendRuleService;

    @Autowired
    private ITaskService taskService;

    @Autowired
    private IAttendSysNoticeTaskService attendSysNoticeTaskService;


    @Override
    @GetMapping(value = "/{id}")
    public Result<AttendRuleVO> findById(@PathVariable String id) {
        TokenUser tokenUser = UserContext.getUser();

        return Result.success(
                attendRuleService.findById(id, tokenUser.getSiteId())
        );
    }

    @Override
    @PostMapping
    public Result<AttendPlanVO> save(@Valid @RequestBody AttendRuleAddDTO attendRuleAddDTO) {
        TokenUser tokenUser = UserContext.getUser();

        return Result.success(
                attendRuleService.save(attendRuleAddDTO, tokenUser)
        );
    }

    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody AttendRuleUpdateDTO attendRuleUpdateDTO) {

        TokenUser tokenUser = UserContext.getUser();

        return Result.success(
                attendRuleService.update(attendRuleUpdateDTO, tokenUser)
        );
    }

    @Override
    @GetMapping
    public Result<Page<AttendRuleListVO>> page(@Valid AttendRuleQuery attendRuleQuery) {
        TokenUser tokenUser = UserContext.getUser();

        return Result.success(
                attendRuleService.selectPage(attendRuleQuery, tokenUser.getSiteId())
        );
    }

    /**
     * 校验人员或者部门中的人员是否已经在考勤规则中
     *
     * @param checkStaffInAttendPlanQuery 校验参数
     * @return
     */
    @Override
    @GetMapping(value="checkStaffInAttendPlan")
    public Result<CheckStaffInAttendPlanVO> checkStaffInAttendPlan(CheckStaffInAttendPlanQuery checkStaffInAttendPlanQuery) {
        TokenUser tokenUser = UserContext.getUser();

        return Result.success(
                attendRuleService.checkStaffInAttendPlan(checkStaffInAttendPlanQuery, tokenUser)
        );
    }

    /**
     * 查询部门
     *
     * @param attendDepInfoTreeQuery 查询部门参数
     * @return
     */
    @Override
    @GetMapping(value="selectDepInfoTree")
    public Result<List<AttendDepInfoTreeVO>> selectDepInfoTree(@Valid AttendDepInfoTreeQuery attendDepInfoTreeQuery) {
        TokenUser tokenUser = UserContext.getUser();

        return Result.success(
                attendRuleService.selectDepInfoTree(attendDepInfoTreeQuery, tokenUser.getSiteId())
        );
    }

    /**
     * 查询部门下的人员
     *
     * @param attendEmpInfoQuery
     * @param siteId
     * @return
     */
    @Override
    @GetMapping(value="selectEmpInfo")
    public Result<List<AttendEmpInfoVO>> selectEmpInfo(AttendEmpInfoQuery attendEmpInfoQuery, String siteId) {
        TokenUser tokenUser = UserContext.getUser();

        return Result.success(
                attendRuleService.selectEmpInfo(attendEmpInfoQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @DeleteMapping(value = "/{attendPlanId}")
    public Result<Integer> delete(@PathVariable String attendPlanId) {
        TokenUser tokenUser = UserContext.getUser();

        return Result.success(
                attendRuleService.deleteAttendPlan(attendPlanId, tokenUser)
        );
    }

    @Override
    @GetMapping(value = "/sendNotice/{attendPlanId}")
    public Result<String> sendNotice(String attendPlanId) {
        attendRuleService.sendNotice(attendPlanId);
        return Result.success();
    }

    @Override
    @GetMapping(value = "/sendWeChatTempMessage/{taskId}")
    public Result<String> sendWeChatTempMessage(String taskId) {
        TaskDO task = taskService.selectDOById(taskId, "");
        if (Objects.nonNull(task)) {
            attendSysNoticeTaskService.runTask(taskId);
        }
        return Result.success();
    }


    @Override
    @GetMapping(value = "/attendPunchRemind")
    public Result<String> attendPunchRemind() {
        attendRuleService.attendPunchRemind();
        return Result.success();
    }

}
